﻿using Common;
using DAL.DBUtility;
using Model.CodeManage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.CodeManage
{
    public class GenCodeBatchDAL
    {
        public ResultEntity AddPatch(GenCodeBatch model)
        {
            string sql = @"insert into CodeManage_GenCodeBatch(EnterpriseId,Batch,CreateLoginID,CreateLoginName,CreateDate,
            ChangeLoginID,ChangeLoginName,ChangeDate,ViewFlag) values(@EnterpriseId,@Batch,@CreateLoginID,@CreateLoginName,@CreateDate,
            @ChangeLoginID,@ChangeLoginName,@ChangeDate,@ViewFlag);select @@IDENTITY;";
            SqlParameter[] parmars = { 
                              new SqlParameter("@EnterpriseId",PageValidate.ConvertToTrimInt(model.EnterpriseId)),
                             
                               new SqlParameter("@Batch",PageValidate.ConvertToTrimString(model.Batch)),
                              
                                  new SqlParameter("@CreateLoginID",PageValidate.ConvertToTrimInt(model.CreateLoginID)),
                                   new SqlParameter("@CreateLoginName",PageValidate.ConvertToTrimString(model.CreateLoginName)),
                                    new SqlParameter("@CreateDate",PageValidate.ConvertToDateTime(model.CreateDate)),
                                     new SqlParameter("@ChangeLoginID",PageValidate.ConvertToTrimInt(model.ChangeLoginID)),
                                      new SqlParameter("@ChangeLoginName",PageValidate.ConvertToTrimString(model.ChangeLoginName)),
                                       new SqlParameter("@ChangeDate",PageValidate.ConvertToDateTime(model.ChangeDate)),
                                         new SqlParameter("@ViewFlag",PageValidate.ConvertToTrimInt(model.ViewFlag))
                                  
                                   };
            return DbHelperSQL.CommonResult(DbHelperSQL.GetSingle(sql, parmars));
        }

        /// <summary>
        /// 获取实体类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public GenCodeBatch getModel(int id)
        {
            GenCodeBatch model = new GenCodeBatch();
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select * from CodeManage_GenCodeBatch with(nolock) where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", id)
			};

            SqlDataReader sdr = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters);
            if (sdr.Read())
            {
                model = CommonModel(sdr);
            }
            sdr.Close();
            return model;

        }

        /// <summary>
        /// 公共Model
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private GenCodeBatch CommonModel(SqlDataReader sdr)
        {
            GenCodeBatch model = new GenCodeBatch();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
            model.EnterpriseId = PageValidate.ConvertToTrimInt(sdr["EnterpriseId"]);
            model.ProductId = PageValidate.ConvertToTrimInt(sdr["ProductId"]);
            model.Batch = PageValidate.ConvertToTrimString(sdr["Batch"]);
            model.ViewFlag = PageValidate.ConvertToTrimInt(sdr["ViewFlag"]);
            model.Sequence = PageValidate.ConvertToTrimInt(sdr["Sequence"]);
            model.CreateLoginID = PageValidate.ConvertToTrimInt(sdr["CreateLoginID"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.ChangeLoginID = PageValidate.ConvertToTrimInt(sdr["ChangeLoginID"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
            model.ProductBatchId = PageValidate.ConvertToTrimInt(sdr["ProductBatchId"]);
            return model;
        }

        public DataTable queryCode(string tbName, string fieldName)
        { 
            string sql = @"select Fw_Code,Wl_Code from {0}";
            sql = string.Format(sql, tbName);
            return DbHelperSQL.GetTable(sql);

        }
    }
}
